library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use ieee.std_logic_unsigned.all;
package mypackage is
	function ffilter(a,b:unsigned) return unsigned;
end package mypackage;

package body mypackage is
	function ffilter(a,b:unsigned) return unsigned is
	variable max:unsigned(7 downto 0);
	variable min:unsigned(7 downto 0);
	variable do,er:unsigned(7 downto 0);
	variable dox2:unsigned(8 downto 0);
	variable apb,er_temp:unsigned(8 downto 0);
	variable maxx2:unsigned(8 downto 0);
	variable max_temp,max_tempc4,max_tempori,min_temp,do_temp:unsigned(9 downto 0);
	variable result:unsigned(15 downto 0);
	begin
	if a>b then
		max:=a;
		min:=b;
	else
		max:=b;
		min:=a;
	end if;
	max_tempc4:="00"&max;--max除以4
	max_tempori:=max&"00";--max后加两个零相当于小数位写零，
	min_temp:="00"&min;--min除以四
	max_temp:=max_tempori-max_tempc4;--max*3/4
	do_temp:=max_temp+min_temp;
	if do_temp(1)='1' then 
		do:=do_temp(9 downto 2)+1;
	else 
		do:=do_temp(9 downto 2);
	end if;
	dox2:=do&'0';--do*2
	maxx2:='0'& max;--把max补成九位与dox2匹配
	apb:=maxx2+min;--max+min
	if dox2>apb then 
		er_temp:=dox2-apb;
		er(7):='0';--按原码的话应该置1，由于仿真时，显示为十进制需要以补码形式操作
		er(6 downto 0):=er_temp(6 downto 0);--所以这三行是对其进行
		er:=(er xor "11111111")+1;--补码运算
	else
		er_temp:=apb-dox2;
		er(7):='0';
		er(6 downto 0):=er_temp(6 downto 0);
	end if;
	result:=do&er;
	return result;
	end function ffilter;
end package body mypackage;	
